package utils

import (
	"strconv"
	"strings"
)

func SplitString(str string, n int) []string {
	var result []string

	for i := 0; i < len(str); i += n {
		end := i + n
		if end > len(str) {
			end = len(str)
		}
		result = append(result, str[i:end])
	}

	return result
}

func CompressAndCleanString(s string) string {
	// 清理字符串：去空格、去换行、删除空白行、删除回车、去除制表符
	s = strings.TrimSpace(s)
	s = strings.ReplaceAll(s, "\r\n", "\n")
	s = strings.ReplaceAll(s, "\r", "\n")
	lines := strings.Split(s, "\n")
	var cleanedLines []string
	for _, line := range lines {
		if strings.TrimSpace(line) != "" {
			cleanedLines = append(cleanedLines, line)
		}
	}
	s = strings.Join(cleanedLines, "")

	// 压缩字符串
	result := ""
	count := 1
	for i := 0; i < len(s); i++ {
		if i == len(s)-1 || s[i] != s[i+1] {
			result += string(s[i])
			result += strconv.Itoa(count)
			count = 1
		} else {
			count++
		}
	}
	if len(result) >= len(s) {
		return s
	}
	return result
}
